public class Solution880 {
    public String decodeAtIndex(String s, int k) {
        long[] f=new long[s.length()];
        while (true){
            f[0]=1;
            if (k==1){
                return s.substring(0,1);
            }
            for (int i=1;i<s.length();i++){
                if (s.charAt(i)>='2'&& s.charAt(i)<='9'){
                    f[i]=f[i-1]*(s.charAt(i)-'0');
                    if (f[i]>=k){
                        k=(int)(k%f[i-1]==0?f[i-1]:k%f[i-1]);
                        break;
                    }
                }else{
                    f[i]=f[i-1]+1;
                    if (k==f[i]){
                        return s.substring(i,i+1);
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution880().decodeAtIndex("abc2",6));
    }
}
